//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import com.baidu.trace.c.d;
import com.baidu.trace.c.g;
import java.io.DataInputStream;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public final class ax {
    private static int a = 0;
    private static Map<Short, g> b = null;

    protected static void a() {
        if(b == null) {
            b = new HashMap();
        }

    }

    public static byte[] a(short var0, int var1) {
        g var7;
        if(b != null && b.containsKey(Short.valueOf(var0))) {
            var7 = (g)b.get(Short.valueOf(var0));
        } else {
            String var2 = (String)n.a.get(Short.valueOf(var0));

            try {
                var7 = (g)Class.forName(var2).newInstance();
            } catch (Exception var5) {
                return null;
            }
        }

        var7.a();
        byte[] var8 = null;

        try {
            var8 = TraceJniInterface.a().buildRequestProtocolData(var0, var1);
        } catch (Exception var4) {
            ;
        }

        if(var8 != null) {
            StringBuffer var3 = new StringBuffer();

            for(int var6 = 0; var6 < var8.length; ++var6) {
                var3.append(var8[var6]);
            }
        }

        return var8;
    }

    public static boolean a(DataInputStream var0) throws Exception {
        byte[] var1 = new byte[]{0};
        boolean var2 = true;

        while(true) {
            if(var2) {
                if(var1[0] == 0 && var0.read(var1) <= 0) {
                    if(++a >= 10) {
                        a = 0;
                        return false;
                    }
                } else {
                    if(var1[0] != -1) {
                        if(++a >= 10) {
                            a = 0;
                            return false;
                        }
                        continue;
                    }

                    a = 0;
                    byte[] var3 = new byte[4];
                    if(var0.read(var3) >= 4) {
                        String var4 = new String(var3);
                        if(!"YING".equals(var4)) {
                            continue;
                        }

                        byte[] var16 = new byte[2];
                        if(var0.read(var16) >= 2) {
                            short var5;
                            if((var5 = d.a(var16, 0)) <= 2) {
                                continue;
                            }

                            byte[] var6 = new byte[(var5 = (short)(var5 - 2)) + 1];
                            int var7;
                            if((var7 = var0.read(var6)) >= var5) {
                                if(var7 <= var5 || var6[var5] != -1) {
                                    var2 = false;
                                }

                                byte[] var20 = new byte[var5 + 2 + 4 + 1];
                                byte var8 = 0;
                                int var21 = var8 + 1;
                                var20[0] = var1[0];
                                if(var2) {
                                    var1[0] = var6[var5];
                                } else {
                                    var1[0] = 0;
                                }

                                int var9;
                                for(var9 = 0; var9 < var3.length; ++var9) {
                                    var20[var21++] = var3[var9];
                                }

                                for(var9 = 0; var9 < var16.length; ++var9) {
                                    var20[var21++] = var16[var9];
                                }

                                for(var9 = 0; var9 < var6.length && var9 < var5; ++var9) {
                                    var20[var21++] = var6[var9];
                                }

                                byte[] var22;
                                if((var22 = TraceJniInterface.a().parseResponseProtocolData(var20)) != null && var22.length != 0) {
                                    String var14 = new String(var22);
                                    boolean var17 = false;

                                    try {
                                        JSONObject var15;
                                        short var18 = (short)(var15 = new JSONObject(var14)).getInt("business_id");
                                        g var19;
                                        if(b != null && b.containsKey(Short.valueOf(var18))) {
                                            var19 = (g)b.get(Short.valueOf(var18));
                                        } else {
                                            var19 = (g)Class.forName((String)n.a.get(Short.valueOf(var18))).newInstance();
                                            b.put(Short.valueOf(var18), var19);
                                        }

                                        var19.a(var15);
                                        continue;
                                    } catch (Exception var12) {
                                        return false;
                                    } finally {
                                        ;
                                    }
                                }

                                return false;
                            }
                        }
                    }
                }
            }

            return true;
        }
    }

    protected static void b() {
        if(b != null) {
            b.clear();
            b = null;
        }

    }
}
